-module ( kinematics ).

-export ( [newWorld/3, destroyWorld/1, propagateToBox/3, propagateToAll/2, dbgVisWorld/2] ).
-export ( [newObject/6, setObject/3, setObjPos/2, setObjVel/2, ctrlObj/2] ).



newWorld( Bounds, CellSize, Overlap ) when
is_tuple(Bounds), size(Bounds)=:=2, is_tuple(CellSize), size(CellSize)=:=3, is_float(Overlap) ->
  spawn( fun() -> kinematics_world:start( Bounds, CellSize, Overlap ) end ).

destroyWorld( World ) when is_pid(World) ->
  World ! destroy.

propagateToBox( World, Box, Msg ) when is_pid(World), is_tuple(Box) ->
  World ! { propagate_to_box, Box, Msg }.

propagateToAll( World, Msg ) when is_pid(World) ->
  World ! { propagate_to_all, Msg }.
  
dbgVisWorld( World, Vis ) when is_pid(World) ->
  propagateToAll( World, { dbg_vis, Vis } ).



newObject( World, P, V, CtrlState, CtrlUpdateFun, CtrlMsgFun ) when
is_pid(World), is_tuple(P), size(P)=:=3, is_tuple(V), size(V)=:=3, is_function(CtrlUpdateFun, 4), is_function(CtrlMsgFun, 4) ->
  spawn_link( kinematics_object, start, [World, P, V, CtrlState, CtrlUpdateFun, CtrlMsgFun] ).

setObject( Obj, P, V ) when is_pid(Obj), is_tuple(P), is_tuple(V) ->
  Obj ! { set_object, P, V }.

setObjPos( Obj, P ) when is_pid(Obj), is_tuple(P) ->
  Obj ! { set_position, P }.
 
setObjVel( Obj, V ) when is_pid(Obj), is_tuple(V) ->
  Obj ! { set_velocity, V }.
 
ctrlObj( Obj, Msg ) when is_pid(Obj) ->
  Obj ! { control_msg, Msg }.
